package com.zbhk.qc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zbhk.qc.dto.ResultVO;
import com.zbhk.qc.dto.SmallProgramVO;
import com.zbhk.qc.entity.VdSmallProgram;
import com.zbhk.qc.service.SmallProgramServer;
import com.zbhk.qc.util.ResultVOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController("SmallProgramController")
@RequestMapping(value = "/smallProgram")
@RequiredArgsConstructor
@Api(tags = "小程序相关接口")
public class SmallProgramController {


    @Autowired
    private SmallProgramServer smallProgramServer;

    @GetMapping("/getListPage")
    @ApiOperation(value = "查询小程序数据", notes = "查询小程序分页数据")
    public IPage<VdSmallProgram> getArticleList(
            @ApiParam(value = "小程序名称") @RequestParam(required = false) String name,
            @ApiParam(value = "小程序appid") @RequestParam(required = false) String appid,
            @ApiParam(value = "页数") @RequestParam(required = true ,defaultValue = "10") Integer pageSize,
            @ApiParam(value = "页码") @RequestParam(required = true,defaultValue = "1") Integer pageNumber) {
        IPage<VdSmallProgram> list = null;
        try {
            list = smallProgramServer.getListPage(name,appid,pageSize,pageNumber);
        }catch (Exception e) {
            return list;
        }
        return list;
    }

    @ApiOperation(value = "新增小程序", notes = "", produces = "application/json")
    @PostMapping("save")
    public ResultVO save(@Valid @ModelAttribute SmallProgramVO smallProgramVO) throws Exception {
        try {
            smallProgramServer.save(smallProgramVO);
        }catch (Exception e){
            return ResultVOUtil.error(e.getMessage());
        }
        return  ResultVOUtil.success(smallProgramVO);

    }

    @ApiOperation(value = "修改小程序", notes = "", produces = "application/json")
    @PostMapping("update")
    public ResultVO update(@Valid @ModelAttribute SmallProgramVO smallProgramVO) throws Exception {
        VdSmallProgram smallProgram = new VdSmallProgram();
        try {
            smallProgram = smallProgramServer.update(smallProgramVO);
        }catch (Exception e){
            return ResultVOUtil.error(e.getMessage());
        }
        return  ResultVOUtil.success(smallProgram);
    }

    @ApiOperation(value = "删除小程序", notes = "", produces = "application/json")
    @GetMapping("delete")
    public ResultVO delete(@ApiParam(value = "小程序编号") @RequestParam(required = true) Long id) throws Exception {
        try {
            smallProgramServer.delete(id);
        }catch (Exception e){
            return ResultVOUtil.error(e.getMessage());
        }
        return  ResultVOUtil.success("成功");
    }

    @ApiOperation(value = "查看小程序", notes = "", produces = "application/json")
    @GetMapping("get")
    public ResultVO get(@ApiParam(value = "小程序编号") @RequestParam(required = true) Long id) throws Exception {
        VdSmallProgram smallProgram = new VdSmallProgram();
        try {
            smallProgram = smallProgramServer.get(id);
        }catch (Exception e){
            return ResultVOUtil.error(e.getMessage());
        }
        return  ResultVOUtil.success(smallProgram);
    }


}
